﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Bar.cs" company="">
//   
// </copyright>
// <summary>
//   The bar.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace Systemathics.FrameWork
{
    [Serializable] public sealed class Bar : EventArgs, ICloneable
    { 
        public Bar(DateTime begintime, DateTime endtime, Decimal open, Decimal high, Decimal low, Decimal close, int size, long volume, int openint)
        {
            try
            {
                BeginTime = begintime;
                EndTime = endtime;
                Duration = endtime - begintime;
                Open = open;
                High = high;
                Low = low;
                Close = close;
                Median = Math.Round((High + Low) / 2, 5);
                OpenInt = openint;
                Size = size;
                Typical = Math.Round((High + Low + Close) / 3, 5);
                Volume = volume;
                Weighted = Math.Round((High + Low + 2 * Close) / 4, 5);
                isComplete = true;
            }
            catch (Exception)
            {
                isComplete = false;
            }
        }

        public Bar()
        {
            BeginTime = new DateTime();
            EndTime = new DateTime();
            Duration = new TimeSpan();
            Open = 0;
            High = 0;
            Low = 0;
            Close = 0;
            Median = 0;
            OpenInt = 0;
            Size = 0;
            Typical = 0;
            Volume = 0;
            Weighted = 0;
            isComplete = false;
        }

        public Guid Id              { get; set; }
        public DateTime BeginTime   { get; set; }
        public TimeSpan Duration    { get; set; }
        public DateTime EndTime     { get; set; }
        public bool isComplete      { get; set; }
        public Decimal Open         { get; set; }
        public Decimal High         { get; set; }
        public Decimal Low          { get; set; }
        public Decimal Close        { get; set; }
        public Decimal Median       { get; set; }
        public int OpenInt          { get; set; }
        public int Size             { get; set; }
        public Decimal Typical      { get; set; }
        public long Volume          { get; set; }
        public Decimal Weighted     { get; set; }

        public double this[BarData barData]
        {
            get
            {
                object result;
                switch (barData)
                {
                    case BarData.Close:
                        result = Close;
                        break;
                    case BarData.High:
                        result = High;
                        break;
                    case BarData.Low:
                        result = Low;
                        break;
                    case BarData.Median:
                        result = Median;
                        break;
                    case BarData.Open:
                        result = Open;
                        break;
                    case BarData.OpenInt:
                        result = OpenInt;
                        break;
                    case BarData.Typical:
                        result = Typical;
                        break;
                    case BarData.Volume:
                        result = Volume;
                        break;
                    case BarData.Weighted:
                        result = Weighted;
                        break;
                    default:
                        result = Double.NaN;
                        break;
                }

                return (double) result;
            }
        }

        #region ICloneable Members

        public object Clone() { return MemberwiseClone(); }

        #endregion
    }
}